package com.itheima.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.ValidateCodeDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.ValidateCodeService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service(interfaceClass = ValidateCodeService.class)
@Transactional
public class ValidateCodeServiceImpl implements ValidateCodeService {
    @Autowired
    private ValidateCodeDao validateCodeDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Result order(Map map) {
        //判断当天是否设置预约
        String orderDate = (String) map.get("orderDate");
        Order od = null;
        try {
            Date order_Date = DateUtils.parseString2Date(orderDate);
            OrderSetting orderSetting = validateCodeDao.findByOrderDate(order_Date);
            if (orderSetting != null) {
                //判断当天是否已经约满
                int number = orderSetting.getNumber();//可预约人数
                int reservations = orderSetting.getReservations();//预约人数
                if (reservations < number) {
                    //判断是否为同一会员同一时间预约同一套餐
                    String telephone = (String) map.get("telephone");
                    Member member = memberDao.findByTelephone(telephone);
                    if (member != null) {
                        String setmealId = (String) map.get("setmealId");
                        Order order = new Order(member.getId(), order_Date, Integer.parseInt(setmealId));
                        Order o = orderDao.findOrder(order);
                        if (o != null) {
                            return new Result(false, MessageConstant.HAS_ORDERED);
                        }
                    } else {
                        //不是会员自动注册为会员
                       member = new Member();
                       member.setPhoneNumber(telephone);
                       member.setName((String) map.get("name"));
                       member.setIdCard((String) map.get("idCard"));
                       member.setSex((String) map.get("sex"));
                       member.setRegTime(new Date());
                        memberDao.add(member);
                    }
                    //预约成功,更新数据库预约人数
                    od = new Order();
                    od.setMemberId(member.getId());//设置会员ID
                    od.setOrderDate(order_Date);//预约日期
                    od.setOrderType((String) map.get("orderType"));//预约类型
                    od.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
                    od.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
                    orderDao.add(od);
                    validateCodeDao.editReservations(orderSetting);


                } else {
                    return new Result(false, MessageConstant.ORDER_FULL);
                }
            } else {
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(true, MessageConstant.ORDER_SUCCESS, od.getId());
    }
}
